﻿Namespace EIBData.EIS
    Public NotInheritable Class EIBDataEIS_12
        Inherits EIBDataEIS_Base

        Protected _D1 As Byte = Nothing
        Protected _D2 As Byte = Nothing
        Protected _D3 As Byte = Nothing
        Protected _D4 As Byte = Nothing
        Protected _D5 As Byte = Nothing
        Protected _D6 As Byte = Nothing
        Protected _Error As Enums.EIBData_Access_Error = Nothing
        Protected _Permission As Enums.EIBData_Access_Permission = Nothing
        Protected _Direction As Enums.EIBData_Access_Direction = Nothing
        Protected _Encryption As Enums.EIBData_Access_Encryption = Nothing
        Protected _Index As Byte = Nothing


        Sub New(data As Byte())
            If Not CanEncode(data) Then Throw New EIB.Exceptions.EIBDataDecodeException("")

            MyBase._data = data
            _D6 = (data(1) And &HF0) >> 4
            _D5 = (data(1) And &HF)
            _D4 = (data(2) And &HF0) >> 4
            _D3 = (data(2) And &HF)
            _D2 = (data(3) And &HF0) >> 4
            _D1 = (data(3) And &HF)
            _Error = ((data(4) And &H80) >> 7)
            _Permission = ((data(4) And &H40) >> 6)
            _Direction = ((data(4) And &H20) >> 5)
            _Encryption = ((data(4) And &H10) >> 4)
            _Index = (data(4) And &HF)
        End Sub

        Sub New(d1 As Byte, d2 As Byte, d3 As Byte, d4 As Byte, d5 As Byte, d6 As Byte, [error] As Enums.EIBData_Access_Error, permission As Enums.EIBData_Access_Permission, direction As Enums.EIBData_Access_Direction, encryption As Enums.EIBData_Access_Encryption, index As Byte)
            If Not [Enum].IsDefined(GetType(Enums.EIBData_Access_Error), [error]) Then Throw New EIB.Exceptions.EIBDataEncodeException("")
            If Not [Enum].IsDefined(GetType(Enums.EIBData_Access_Permission), permission) Then Throw New EIB.Exceptions.EIBDataEncodeException("")
            If Not [Enum].IsDefined(GetType(Enums.EIBData_Access_Direction), direction) Then Throw New EIB.Exceptions.EIBDataEncodeException("")
            If Not [Enum].IsDefined(GetType(Enums.EIBData_Access_Encryption), encryption) Then Throw New EIB.Exceptions.EIBDataEncodeException("")

            _D6 = d6
            _D5 = d5
            _D4 = d4
            _D3 = d3
            _D2 = d2
            _D1 = d1
            _Error = [error]
            _Permission = permission
            _Direction = direction
            _Encryption = encryption
            _Index = index
            MyBase._data = New Byte() {0,
                                      ((_D6 And &HF) << 4) + (_D5 And &HF),
                                      ((_D4 And &HF) << 4) + (_D3 And &HF),
                                      ((_D2 And &HF) << 4) + (_D1 And &HF),
                                      (If(_Error, 1, 0) << 7) + (If(_Permission, 1, 0) << 6) + (If(_Direction, 1, 0) << 5) + (If(_Encryption, 1, 0) << 4) + (_Index And &HF)
                                     }
        End Sub


        Public Overrides Function CanEncode(data() As Byte) As Boolean
            Return data IsNot Nothing AndAlso data.Length = EIS_OctetLength
        End Function

        Public Overrides Function ToString() As String
            Return String.Format("D1: {0}, D2: {1}, D3: {2}, D4: {3}, D5: {4}, D6: {5}, Error: {6}, Permission: {7}, Direction: {8}, Encrytion: {9}, Index: {10}", _D1, _D2, _D3, _D4, _D5, _D6, _Error, _Permission, _Direction, _Encryption, _Index)
        End Function

        Public Overrides Function ToObject() As Object()
            Return New Object() {_D1, _D2, _D3, _D4, _D5, _D6, _Error, _Permission, _Direction, _Encryption, _Index}
        End Function

        Public Overrides ReadOnly Property ObjectType As System.Type()
            Get
                Return New Type() {GetType(Byte), GetType(Byte), GetType(Byte), GetType(Byte), GetType(Byte), GetType(Byte), GetType(Enums.EIBData_Access_Error), GetType(Enums.EIBData_Access_Permission), GetType(Enums.EIBData_Access_Direction), GetType(Enums.EIBData_Access_Encryption), GetType(Byte)}
            End Get
        End Property

        Public Overrides Function ToInfoList() As List(Of EIB.InfoBase)
            Dim list As New List(Of EIB.InfoBase)

            list.Add(New EIB.Info("Type", EIS_Name))
            list.Add(New EIB.Info("D1", _D1))
            list.Add(New EIB.Info("D2", _D2))
            list.Add(New EIB.Info("D3", _D3))
            list.Add(New EIB.Info("D4", _D4))
            list.Add(New EIB.Info("D5", _D5))
            list.Add(New EIB.Info("D6", _D6))
            list.Add(New EIB.Info("Error", String.Format("{0} (0x{0:X})", _Error)))
            list.Add(New EIB.Info("Permission", String.Format("{0} (0x{0:X})", _Permission)))
            list.Add(New EIB.Info("Direction", String.Format("{0} (0x{0:X})", _Direction)))
            list.Add(New EIB.Info("Encryption", String.Format("{0} (0x{0:X})", _Encryption)))

            Return list
        End Function

        Public Shared Shadows ReadOnly Property EIS_OctetLength As Byte
            Get
                Return 5
            End Get
        End Property

        Public Shared Shadows ReadOnly Property EIS_Name As String
            Get
                Return "EIS 12"
            End Get
        End Property


        Public ReadOnly Property D1 As Byte
            Get
                Return _D1
            End Get
        End Property

        Public ReadOnly Property D2 As Byte
            Get
                Return _D2
            End Get
        End Property

        Public ReadOnly Property D3 As Byte
            Get
                Return _D3
            End Get
        End Property

        Public ReadOnly Property D4 As Byte
            Get
                Return _D4
            End Get
        End Property

        Public ReadOnly Property D5 As Byte
            Get
                Return _D5
            End Get
        End Property

        Public ReadOnly Property D6 As Byte
            Get
                Return _D6
            End Get
        End Property

        Public ReadOnly Property [Error] As Enums.EIBData_Access_Error
            Get
                Return _Error
            End Get
        End Property

        Public ReadOnly Property Permission As Enums.EIBData_Access_Permission
            Get
                Return _Permission
            End Get
        End Property

        Public ReadOnly Property Direction As Enums.EIBData_Access_Permission
            Get
                Return _Direction
            End Get
        End Property

        Public ReadOnly Property Encryption As Enums.EIBData_Access_Encryption
            Get
                Return _Encryption
            End Get
        End Property

        Public ReadOnly Property Index As Byte
            Get
                Return _Index
            End Get
        End Property
    End Class
End Namespace